package com.higgins.eventtracking.client.health.fundamentals;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.visualizations.LineChart;
import com.higgins.eventtracking.client.EventTrackingService;
import com.higgins.eventtracking.client.EventTrackingServiceAsync;
import com.higgins.eventtracking.client.dto.HealthEventDTO;
import com.higgins.eventtracking.client.dto.HealthStatsDTO;
import com.higgins.eventtracking.client.gui.components.AnnotatedChart;
import com.higgins.eventtracking.server.DateUtility;

public class FundamentalsChart extends AnnotatedChart	{
	public void execute(String chartName)	{
		setTitle(chartName);
		setColumnA("Date");
		setColumnB(chartName);
		setColumnC(null);
		setScale(10);
		if (chartName.equals("Health Points"))	{
			setScale(200);
		}
		executeChart();
	}
	
	public void executeChart()	{
		if (columnB.equals("Health Points"))	{
			Date now = new Date();
			Date start = new Date();
			start.setMonth(start.getMonth()-2);
			greetingService.retrieveHealthStats(start, now,
					new AsyncCallback<List<HealthStatsDTO>>() {
						public void onFailure(Throwable caught) {
							displayChartStats(new ArrayList<HealthStatsDTO>());
						}
						public void onSuccess(final List<HealthStatsDTO> results) {
							displayChartStats(results);
						}
					});
		} else if (columnC!=null)	{
			greetingService.retrieveHealthEvents(columnB, columnC, new AsyncCallback<List<HealthEventDTO>>()  {
				@Override
				public void onFailure(Throwable caught) {
				}
				@Override
				public void onSuccess(List<HealthEventDTO> result) {
					displayChart(result);
				}  });
		} else {
			greetingService.retrieveHealthEvents(columnB, new AsyncCallback<List<HealthEventDTO>>()  {
				@Override
				public void onFailure(Throwable caught) {
				}
				@Override
				public void onSuccess(List<HealthEventDTO> result) {
					displayChart(result);
				}  });
		}
	}
	public void displayChartStats(final List<HealthStatsDTO> result)	{
        VisualizationUtils.loadVisualizationApi(new Runnable() {
            @Override
            public void run() {
                        JavaScriptObject jdygraph = createDygraph(
                                annotatedChart.getElement(), convertToDataTableStats(result), 0,
                                scale);
            }
        }, LineChart.PACKAGE);
	}
	public void displayChart(final List<HealthEventDTO> result)	{
        VisualizationUtils.loadVisualizationApi(new Runnable() {
            @Override
            public void run() {
                        JavaScriptObject jdygraph = createDygraph(
                                annotatedChart.getElement(), convertToDataTable(result), 0,
                                scale);
            }
        }, LineChart.PACKAGE);
	}

	private DataTable convertToDataTable(List<HealthEventDTO> results)	{
	    DataTable data = DataTable.create();
	    data.addColumn(ColumnType.DATE, columnA);
	    data.addColumn(ColumnType.NUMBER, columnB);
	    if (columnC!=null)	{
		    data.addColumn(ColumnType.NUMBER, columnC);
	    }
	    data.addRows(results.size());
	    int row = 0;
	    int col = 0;

	    for (HealthEventDTO dto:results)	{
	    	data.setValue(row, col++, dto.getTimestamp());

	    	if (dto.getHealthEventType().equals(columnB))	{
		    	data.setValue(row, col++, dto.getHealthEventIntensityScale());
	    	} else {
	    		Integer nullInt = null;
		    	data.setValue(row, col++, nullInt);
	    	}

	    	if (columnC!=null && dto.getHealthEventType().equals(columnC))	{
		    	data.setValue(row, col++, dto.getHealthEventIntensityScale());
	    	} else if (columnC!=null) {
	    		Integer nullInt = null;
		    	data.setValue(row, col++, nullInt);
	    	}
	    	row++;
	    	col = 0;
	    }
		return data;
	}
	
	  private DataTable convertToDataTableStats(List<HealthStatsDTO> dtos) {
	    DataTable data = DataTable.create();
	    data.addColumn(ColumnType.DATE, "Date");
	    data.addColumn(ColumnType.NUMBER, "Health Points");
	    data.addRows(dtos.size());
	    
	    int count = 0;
	    for (HealthStatsDTO dto:dtos)	{
	        data.setValue(count, 0, dto.getDate());
	        data.setValue(count, 1, dto.getHealthPoints());
	        count++;
	    }
	    
	    return data;
	  }	

}
